package com.codez.jwt;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.codez.util.JwtUtils;

import io.jsonwebtoken.Claims;

/**
 *  @author codez
 *	@createTime 2019-05-26 11:59:00
 */


@RestController
@RequestMapping("jwt")
public class TestJwtController {

	@Autowired
	JwtUtils jwtUtils;
	
	@RequestMapping
	public String test(HttpServletRequest request) {
		String auth = request.getHeader("Authentication");
		if(StringUtils.isEmpty(auth)) {
			return "未登录,请先登陆!";
		}
		Claims claims = jwtUtils.parseJwt(auth);
		return "my-username : " + claims.get("my-username");
	}
	
	@RequestMapping("login")
	public String login(String username , String id) {
		Map<String, Object> map = new HashMap<>();
		map.put("my-username", username);
		map.put("my-id", id);
		String token = jwtUtils.createJwt(id, username, map);
		return "登陆成功! token:"+token;
	}
	
}
